package com.kele.controller;

import com.kele.domain.Blog;
import com.kele.domain.Collections;
import com.kele.service.BlogService;
import com.kele.service.CollectionService;
import com.kele.utils.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 栗守佳
 */
@Controller
@RequestMapping("/collection")
public class CollectionController {
    @Resource
    private CollectionService collectionService;
    @Resource
    private BlogService blogService;

    /**
     * 文章收藏功能
     * @author 栗守佳
     * @param blogid
     * @param userid
     * @return
     */
    @RequestMapping(value = "/Collection", method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "博客id", name = "blogid", required = true, type = "int"),
            @ApiImplicitParam(value = "博客id", name = "userid", required = true, type = "int")
    })
    public ResponseData Collection(int blogid, int userid) {
        //先查询是否存在收藏记录，然后再进行判断
        Collections cc = collectionService.findCollection(blogid, userid);
        if (cc != null) {
            //删除收藏记录.
            int count = collectionService.deleteCollection(cc.getCollectionid());
            System.out.println("删除成功，删除的行数是" + count);
            Blog blog = blogService.findBlogById(cc.getBlogid());
            //操作数据库
            blogService.subCollection(blog.getBid());
            return ResponseData.success("取消收藏成功", blog.getCollection() - 1);
        } else {
            Blog blog = blogService.findBlogById(blogid);
            Collections collection = new Collections();
            collection.setBlogid(blogid);
            collection.setUserid(userid);
            collection.setBlogname(blog.getTitle());
            collection.setBlogtype(blog.getType());
            //在collection中添加新的收藏记录
            Integer integer = collectionService.insertCollection(collection);
            //操作数据库
            blogService.addCollection(blog.getBid());
            return ResponseData.success("收藏成功", blog.getCollection() + 1);
        }

    }


    /**
     * 用户个人中心我的收藏
     * @author 栗守佳
     * @param userid
     * @return
     */
    @RequestMapping(value = "/findBloggerCollection", method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userid", required = true, type = "int")

    })
    public ResponseData findBloggerCollection(int userid) {
        List<Collections> collectionsList = collectionService.findBloggerCollection(userid);
        if (collectionsList.size() != 0) {
            return ResponseData.success("查询成功", collectionsList);

        } else {
            return ResponseData.fail("您还未有收藏记录哦");
        }
    }


}
